package com.google.code.head2head.dao;


import java.util.Arrays;
import java.util.List;

import javax.persistence.Query;

import com.google.code.head2head.model.Entry;

public class EntryDao extends AbstractDao<Entry>{
	
	public Entry getEntryWithGrades(Long id){
		String sql = "Select Object(e) from Entry e left join fetch e.grades where e.id=?1";
		Query q = getEntityManager().createQuery(sql);
		q.setParameter(1, id);
		return (Entry)q.getSingleResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Entry> getEntriesWithTags(String... tags){
		Arrays a;
		String sql = "Select distinct e from Entry e left join fetch e.tags as tag where tag in (";
		for (int i = 0; i < tags.length; i++) {
			sql+="'"+tags[i]+"'";
			if(i<tags.length-1){
				sql+=",";
			}
		}
		sql+=")";
		Query q = getEntityManager().createQuery(sql);
		return q.getResultList();
	}
	
}
